package org.xmdl.silk.wrap;

import java.util.List;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.xmdl.silk.Method;
import org.xmdl.silk.Statement;
import org.xmdl.silk.Variable;
import org.xmdl.silk.impl.MethodImpl;
import org.xmdl.silk.visit.Visitor;

public class MethodWrap extends MethodImpl implements Wrapped {

	private final Method wrap;

	private EList<Statement> wStatements;

	private EList<Variable> wParameters;
	
	public MethodWrap(Method wrap) {
		this.wrap = wrap;
	}

	@Override
	public String getName() {
		return wrap.getName();
	}

	@Override
	public void setName(String newName) {
		wrap.setName(newName);
	}

	@Override
	public EList<Statement> getStatements() {
		if (wStatements == null) {
			wStatements = new BasicEList<Statement>();
			for (Statement c : wrap.getStatements()) {
				Statement cw = WrapUtils.statement(c);
				wStatements.add(cw);
			}
		}
		return wStatements;
	}

	@Override
	public EList<Variable> getParameters() {
		if (wParameters == null) {
			wParameters = new BasicEList<Variable>();
			for (Variable c : wrap.getParameters()) {
				Variable cw = WrapUtils.variable(c);
				wParameters.add(cw);
			}
		}
		return wParameters;
	}
	
	public boolean accept(Visitor visitor) {
		if (visitor.visitEnter(this)) {
			final List<Statement> children = getStatements();
			for (Statement child : children) {
				if (!((Wrapped) child).accept(visitor))
					break;
			}
		}
		boolean result = visitor.visitLeave(this);
		return result;
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(getName()).append("(");
		sb.append(")");
		return sb.toString();
	}
	
	
}
